package com.putable.nmc.dmv.tests;

import junit.framework.TestCase;

import com.putable.nmc.dmv.CustType;
import com.putable.nmc.dmv.Customer;
import com.putable.nmc.dmv.CustomerLine;
import com.putable.nmc.world.CIT;
import com.putable.nmc.world.CustomerType;
import com.putable.nmc.world.World;
import com.putable.pqueue.PQueue;
import com.putable.pqueue.PQueueImpl;

public class TestCustomerLine extends TestCase {
	
	public void testAddRemove(){
		CustomerLine cl = new CustomerLine();
		for(int i = 0; i < 25; i++){
			Customer c = new Customer();
			c.setType(CustomerType.BIKER);
			cl.add(c, 0);
		}
		for(int i = 0; i < 25; i++){
			Customer c = new Customer();
			c.setType(CustomerType.STUDENT);
			cl.add(c, 0);
		}
		
		for(int i = 0; i < 25; i++){
			assertEquals(CustType.BIKER, cl.remove().getType());
		}
		for(int i = 0; i < 25; i++){
			assertEquals(CustType.STUDENT, cl.remove().getType());
		}
		
	}
	
	public void testDelete(){
		CustomerLine cq = new CustomerLine();
		Customer c2 = new Customer();
		cq.add(c2, 0);
		cq.delete(c2, 0);
		
		for(int i = 0; i < 25; i++){
			Customer c = new Customer();
			c.setType(CustomerType.RETIREE);
			cq.add(c, 0);
		}
		Customer c = new Customer();
		c.setType(CustomerType.STUDENT);
		cq.add(c, 0);
		for(int i = 0; i < 25; i++){
			Customer b = new Customer();
			b.setType(CustomerType.RETIREE);
			cq.add(b, 0);
		}
		
		cq.delete(c, 0);
		for(int i = 0; i < 50; i++){
			assertEquals(CustType.RETIREE, cq.remove().getType());
		}
	}
	
	public void testIsEmpty(){
		CustomerLine cq = new CustomerLine();
		assertTrue(cq.isEmpty());
		cq.add(new Customer(), 0);
		assertFalse(cq.isEmpty());
		cq.remove();
		assertTrue(cq.isEmpty());
		cq.add(new Customer(), 0);
		assertFalse(cq.isEmpty());
	}
	
	public void testGetSize(){
		CustomerLine cl = new CustomerLine();
		Customer c = new Customer();
		for(int i = 0; i < 25; i++){
			c = new Customer();
			cl.add(c, 0);
		}
		assertEquals(25, cl.getSize());
		for(int i = 0; i < 20; i++){
			cl.remove();
		}
		assertEquals(5, cl.getSize());
		cl.delete(c, 0);
		assertEquals(4, cl.getSize());
		for(int i = 0; i < 246; i++){
			c = new Customer();
			cl.add(c, 0);
		}
		assertEquals(250, cl.getSize());
	}
	
	public void testRetireeTalk(){
		World world = new World();
		double time = 0;
		CIT cit = world.getNextCIT(time);
		PQueue pq = new PQueueImpl();
		CustomerLine cLine = new CustomerLine();
		//Test the retiree talk factor
		Customer r1 = new Customer(pq, cit, world);
		r1.setType(CustomerType.RETIREE);
		pq.insert(r1);
		cLine.add(r1, time);
		time = r1.getTimeOfNextTask();
		Customer r2 = new Customer(pq,cit,world);
		pq.insert(r2);
		r2.setType(CustomerType.RETIREE);
		cLine.add(r2, time);
		assertEquals(time + r1.getType().retireeTalkFactor, r1.getTimeOfNextTask());
	}
	
	public void testRetireeFear(){
		World world = new World();
		double time = 0;
		CIT cit = world.getNextCIT(time);
		PQueue pq = new PQueueImpl();
		CustomerLine cLine = new CustomerLine();
		Customer r = new Customer(pq, cit, world);
		//Test the add method of a biker
		r.setType(CustomerType.RETIREE);
		pq.insert(r);
		cLine.add(r, time);
		time = r.getTimeOfNextTask();
		Customer b = new Customer(pq,cit,world);
		b.setType(CustomerType.BIKER);
		pq.insert(b);
		r.setTimeOfNextTask(time + 10);
		cLine.add(b, time);
		assertEquals(time+10-r.getType().retireeFearFactor, r.getTimeOfNextTask());
		
		//Test deleting a biker
		Customer b2 = new Customer(pq,cit,world);
		pq.insert(b2);
		b2.setType(CustomerType.BIKER);
		cLine.add(b2, time);
		assertEquals(time+10-r.getType().retireeFearFactor, r.getTimeOfNextTask());
		cLine.delete(b, time);
		assertEquals((int)((time+10-(2*r.getType().retireeFearFactor))*100), (int)(r.getTimeOfNextTask()*100)+1);
		/* It somehow got 9.379999999999999 instead of 9.38 so I had to do some ridiculous things
		 * to make it work because it is essentially the same number... Why do you do this to me java!?
		 */
	}
}
